package com.jxs.android.cmcc;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;

import com.jxs.android.cmcc.data.KipInfo;
import com.jxs.android.cmcc.db.KipConfigHolder;
import com.jxs.android.cmcc.db.KipHolder;
import com.jxs.android.util.StartHelper;
import com.jxs.android.weiget.TopBar;

public class KipSelectGrid extends KipSelectAbstract {

	private GridView appGridView = null;
	private Context context;

	private List<KipInfo> kipInfos = new ArrayList<KipInfo>();

	private KipGdAdpter kipGdAdpter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.bu_select_kip_gd);
		context = this;
		appGridView = (GridView) findViewById(R.id.main_kip_gridview);
		kipGdAdpter = new KipGdAdpter(context);
		appGridView.setAdapter(kipGdAdpter);
	}

	/**
	 * @Des 点击中间的图片的时候处理事件
	 */
	private void toTake(final int position1, final KipInfo kipInfo) {
		CharSequence[] items = { "删除" };
		new AlertDialog.Builder(this).setTitle("配置指标").setItems(items, new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				KipHolder.deleteKipInfo(kipInfo.getKpi_id());
				KipAddOrDel("delKpi", position1, kipInfo);
			}
		}).create().show();
	}

	@Override
	protected void onResume() {
		super.onResume();
		refresh(KipConfigHolder.Scope.SELECTED);
	}

	@Override
	public void initTopBarReturn(TopBar topBar) {
		if (null != topBar) {
			topBar.setTopBarReturnListern(new TopBar.ReturnBtnListern() {
				@Override
				public void onClickReturn() {
					setResult();
				}
			});
		}
	}

	@Override
	public void onBackPressed() {
		setResult();
	}

	private void setResult() {
		setResult(RESULT_OK);
		finish();
	}

	private class KipGdAdpter extends BaseAdapter {

		private LayoutInflater inflater = null;

		public KipGdAdpter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return kipInfos.size();
		}

		@Override
		public KipInfo getItem(int position) {
			return kipInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			final KipInfo kipInfo = kipInfos.get(position);
			if ("-1".equals(kipInfo.getKpi_id())) {
				ImageButton imgBtn = (ImageButton) inflater.inflate(R.layout.bu_select_pic_gd_item_ibtn, null);
				imgBtn.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						StartHelper.startActivity(KipSelectList.class);
					}
				});
				return imgBtn;
			} else {
				Button button = (Button) inflater.inflate(R.layout.bu_select_pic_gd_item_btn, null);
				button.setText(kipInfos.get(position).getKpi_name());
				button.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						toTake(position, kipInfos.get(position));
					}
				});
				return button;
			}
		}
	}

	@Override
	protected void handlerResult(List<KipInfo> kipInfos) {
		KipInfo kipInfo = new KipInfo();
		kipInfo.setKpi_id("-1");
		kipInfos.add(kipInfo);

		this.kipInfos.clear();
		this.kipInfos.addAll(kipInfos);
		kipGdAdpter.notifyDataSetChanged();
	}

	@Override
	protected void nextStep(int position) {
		refresh(KipConfigHolder.Scope.SELECTED);
	}

	@Override
	protected String getTopBarTitle() {
		return "指标配置";
	}

}